/*
 * Copyright (c) 2011, Nikolaus Moll
 * All rights reserved.
 */
package net.miblounge.gradecalculator.core.serialization;

import javax.xml.bind.annotation.adapters.XmlAdapter;

import net.miblounge.gradecalculator.core.enums.ExaminationType;
import net.miblounge.gradecalculator.core.enums.ExaminationTypeList;

public final class ExaminationTypeListAdapter extends XmlAdapter<String, ExaminationTypeList>  {

	@Override
	public ExaminationTypeList unmarshal(final String v) throws Exception {
		final ExaminationTypeList result = new ExaminationTypeList();
		if (!v.equals("")) { 
			final String[] values = v.split(" ");
			for (final String value : values) {
				if (value.equals("None")) {
					continue;
				}

				final ExaminationType type = ExaminationType.valueOf(value);
				if (type == null) {
					final ExaminationType tmpType = new ExaminationType(value);
					ExaminationType.addExaminationType(tmpType);
					result.add(tmpType);
				} else {
					result.add(type);
				}
			}
		}
		return result;
	}

	@Override
	public String marshal(final ExaminationTypeList v) throws Exception {
		String result = "";
		for (ExaminationType type : v) {
			result = result + " " + type.getAbbreviation();
		}
		return result.trim();
	}

}